package com.rongxun.hbwallet.mvp.presenter;


import android.graphics.Bitmap;
import android.support.annotation.NonNull;

import com.rongxun.base.BasePresenter;
import com.rongxun.base.ResponseSubscriber;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.mvp.contract.IWalletDetail;
import com.rongxun.scancode.qrcode.QRCodeEncode;
import com.rongxun.utils.FlyLog;
import com.rongxun.utils.RxUtil;
import com.rongxun.utils.UUi;
import com.rongxun.utils.wallet.ETHWallet;
import com.rongxun.utils.wallet.ETHWalletUtils;
import com.rongxun.utils.wallet.WalletDaoUtils;
import com.rongxun.utils.wallet.WalletSubscriber;

import java.io.ByteArrayOutputStream;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.functions.Consumer;

/**
 * Created by dwq on 2018/3/26/026.
 * e-mail:lomapa@163.com
 */

public class WalletDetailPresenter extends BasePresenter<IWalletDetail.IView, IWalletDetail.IModel> implements IWalletDetail.IPresenter {

    public WalletDetailPresenter(@NonNull IWalletDetail.IView pUi) {
        super(pUi);
    }

    @Override
    public void modifyWalletName(long walletId, String name) {
        WalletDaoUtils.updateWalletName(walletId, name);
        mView.modifySuccess();
    }

    @Override
    public void modifyWalletPwd(final long walletId, final String walletName, final String oldPassword, final String newPassword) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<ETHWallet>) e -> {
            e.onNext(ETHWalletUtils.modifyPassword(walletId, walletName, oldPassword, newPassword));
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<ETHWallet>(getView()) {
                    @Override
                    public void onSuccess(ETHWallet ethWallet) {
                        getView().modifyPwdSuccess(ethWallet);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));

    }

    @Override
    public void deriveWalletPrivateKey(final long walletId, final String password) {

        addSubscribe(Flowable.create((FlowableOnSubscribe<String>) e -> {
            e.onNext(ETHWalletUtils.derivePrivateKey(walletId, password));
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<String>(getView()) {
                    @Override
                    public void onSuccess(String privateKey) {
                        mView.showDerivePrivateKeyDialog(privateKey);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public void deriveWalletKeystore(final long walletId, final String password) {
        addSubscribe(Flowable.create((FlowableOnSubscribe<String>) e -> {
            e.onNext(ETHWalletUtils.deriveKeystore(walletId, password));
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<String>(getView()) {
                    @Override
                    public void onSuccess(String keystore) {
                        mView.showDeriveKeystore(keystore);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public void deleteWallet(final long walletId) {

        addSubscribe(Flowable.create((FlowableOnSubscribe<Boolean>) e -> {
            e.onNext(ETHWalletUtils.deleteWallet(walletId));
            e.onComplete();
        }, BackpressureStrategy.BUFFER)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new WalletSubscriber<Boolean>(getView()) {
                    @Override
                    public void onSuccess(Boolean isDelete) {
                        mView.deleteSuccess(isDelete);
                    }

                    @Override
                    public void disposeEorCode(String message, String code) {

                    }
                }));
    }

    @Override
    public void createQRCode(String address) {

        addSubscribe(Flowable.create((FlowableOnSubscribe<byte[]>) e -> {
            QRCodeEncode.Builder builder = new QRCodeEncode.Builder();
            builder.setBackgroundColor(0xffffff)
                    .setOutputBitmapHeight(UUi.dip2px(getView().getContext().getResources().getDimension(R.dimen.x402)))
                    .setOutputBitmapWidth(UUi.dip2px(getView().getContext().getResources().getDimension(R.dimen.x402)));
            Bitmap qrCodeBitmap = builder.build().encode(address);


            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            qrCodeBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] bytes = baos.toByteArray();
            e.onNext(bytes);
            e.onComplete();
        }, BackpressureStrategy.BUFFER).compose(RxUtil.rxSchedulerHelper()).subscribe(new Consumer<byte[]>() {
            @Override
            public void accept(byte[] bytes) throws Exception {
                getView().showQRCode(bytes);
            }
        }));

    }

    @Override
    public void deleteWallet(String walletId) {
        if (walletId.isEmpty()) {
            FlyLog.i("deleteWallet walletId is Null");
            return;
        }
        addSubscribe(getModel().deleteWallet(walletId).subscribeWith(new ResponseSubscriber<String>(getView()) {
            @Override
            public void onSuccess(String s) {
                getView().showDeleteWalletResult();
            }

            @Override
            public void disposeEorCode(String message, String code) {

            }
        }));
    }
}
